#ifndef StarSelect_H
#define StarSelect_H

#include <QAbstractTableModel>
#include <utility>

#include <StarEntry.h>
#include <Catalog.h>

#define MIN_SOUTH_ALT 10.0
#define MAX_SOUTH_ALT 40.0
#define MIN_NORTH_ALT 10.0
#define MAX_NORTH_ALT 40.0
#define MAX_AZ 20.0
#define MAG_LIMIT 6.0

#define SIDE(azimuth) (((azimuth) <= 180.0)?"E":"W")

class StarSelect: public QAbstractTableModel
{

  Q_OBJECT


  
 
public slots:
  void setSite(double lng, double lat);
  void setLST(double lst);

public:
    struct star {
    StarEntry *starentry;
    double alt, az;
    double ranow, denow;
    star(StarEntry *s, double calt, double caz, double cranow, double cdenow) {
      starentry = s;
      alt=calt;
      az=caz;
      ranow=cranow;
      denow=cdenow;
    }
  };
  
  typedef std::pair<star *, star *> alignpair;
   
  void setCatalog(Catalog *cat);
  void actualizeAltAz(double nlst=0.0);
  void computePairs();
  alignpair getPair(QModelIndex index);
  
  int rowCount(const QModelIndex& parent) const;
  int columnCount(const QModelIndex& parent) const;
  QVariant data(const QModelIndex& index, int role) const;
  QVariant headerData(int section, Qt::Orientation orientation, int role) const;

protected:
  struct starcomp {
  bool operator() (star *const& lhs, star *const& rhs) const
  {return lhs->starentry->htmid <rhs->starentry->htmid;}
  };

  struct starmag {
  bool operator() (star *const& lhs, star *const& rhs) const
  {return lhs->starentry->vmag > rhs->starentry->vmag;}
  };  
  
 
  
  struct stsortstarpair {
  bool operator() (alignpair lhs, alignpair rhs) 
  { return ((lhs.first->starentry->vmag + lhs.second->starentry->vmag) <
            (rhs.first->starentry->vmag + rhs.second->starentry->vmag));
  }
  } sortstarpair;
  
  double lng, lat;
  double lst;
  double actualizelst;
  
  Catalog *catalog;
  std::set<star *, starcomp> visibleStars;
  std::set<star *, starmag> alignSWStars;
  std::set<star *, starmag> alignNWStars;
  std::set<star *, starmag> alignSEStars;
  std::set<star *, starmag> alignNEStars;
  std::vector<alignpair> alignPairs;
  
};

#endif //StarSelect_H